package com.ouwen.smartpay.service.chain.slots.idempotent;

import com.fm.core.common.boot.Apps;
import com.ouwen.smartpay.entity.OrderInfo;
import com.ouwen.smartpay.openapi.order.ServiceOrder;
import com.ouwen.smartpay.service.OrderInfoService;
import com.ouwen.smartpay.service.chain.context.Context;
import com.ouwen.smartpay.service.chain.slotchain.AbstractLinkedProcessorSlot;
import org.springframework.context.ApplicationContext;

/**
 * @author <a href="http://youngitman.tech">青年IT男</a>
 * @version v1.0.0
 * @className IdempotentSlot
 * @description 幂等性校验:返回存在订单终态值
 * @JunitTest: {@link  }
 * @date 2020-04-26 11:06
 **/
public class IdempotentDirectReturnSlot extends AbstractLinkedProcessorSlot<ServiceOrder> {

    @Override
    public void entry(Context context, ServiceOrder order, Object... args) throws Throwable {

        ApplicationContext applicationContext = Apps.getApplicationContext();

        OrderInfoService orderInfoService = applicationContext.getBean(OrderInfoService.class);

        //每一次请求请求流水号必须唯一
        OrderInfo orderInfo = orderInfoService.findOrderInfoByPartnerIdAndAndSubParentIdRequestNo(order.getPartnerId(), order.getSubPartnerId(), order.getRequestNo());

        //存在重复订单，直接返回
        if (orderInfo != null) {

            context.setIdempotentBreak(true);

            exit(context, order, args, null);

        } else {

            fireEntry(context, order, args);

        }

    }

    @Override
    public void exit(Context context, Object... args) {

        fireExit(context, args);

    }
}
